package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.NoticeAdminMapper;
import org.linlinjava.litemall.db.models.NoticeAdmin;
import org.linlinjava.litemall.db.service.NoticeAdminService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class NoticeAdminServiceImpl extends ServiceImpl<NoticeAdminMapper, NoticeAdmin>
        implements NoticeAdminService {

    @Override
    public IPage<NoticeAdmin> querySelective(String title, String type, Integer adminId, Integer pageNo, Integer pageSize) {
        Page<NoticeAdmin> page = new Page<>(pageNo, pageSize);
        QueryWrapper<NoticeAdmin> query = new QueryWrapper<>();
        query.like(!StringUtils.isEmpty(title), "title", title);
        query.eq("admin_id", adminId);
        if(type.equals("read")){
         query.isNotNull("read_time");
        }
        else if(type.equals("unread")){
            query.isNull("read_time");
        }
        return baseMapper.selectPage(page, query);
    }

    @Override
    public NoticeAdmin find(Integer noticeId, Integer adminId) {
        QueryWrapper<NoticeAdmin> query = new QueryWrapper<>();
        query.eq("admin_id", adminId);
        query.eq("notice_id", noticeId);
        return baseMapper.selectOne(query);
    }

    @Override
    public void markReadByIds(List<Integer> ids, Integer adminId) {
        UpdateWrapper<NoticeAdmin> update = new UpdateWrapper<>();
        update.eq("admin_id", adminId);
        update.in("id", ids);
        update.set("read_time", LocalDateTime.now());
        baseMapper.update(null, update);
    }

    @Override
    public void deleteById(Integer id, Integer adminId) {
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        deleteByIds(ids, adminId);
    }

    @Override
    public void deleteByIds(List<Integer> ids, Integer adminId) {
        UpdateWrapper<NoticeAdmin> update = new UpdateWrapper<>();
        update.eq("admin_id", adminId);
        update.in("id", ids);
        NoticeAdmin noticeAdmin = new NoticeAdmin();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);
        baseMapper.update(noticeAdmin, update);
    }

    @Override
    public int countUnread(Integer adminId) {
        QueryWrapper<NoticeAdmin> query = new QueryWrapper<>();
        query.eq("admin_id", adminId);
        query.isNull("read_time");
        return baseMapper.selectCount(query);
    }

    @Override
    public List<NoticeAdmin> queryByNoticeId(Integer noticeId) {
        QueryWrapper<NoticeAdmin> query = new QueryWrapper<>();
        query.eq("notice_id", noticeId);
        return baseMapper.selectList(query);
    }

    @Override
    public void deleteByNoticeId(Integer id) {
        QueryWrapper<NoticeAdmin> query = new QueryWrapper<>();
        query.eq("notice_id", id);
        NoticeAdmin noticeAdmin = new NoticeAdmin();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);
        baseMapper.update(noticeAdmin, query);
    }

    @Override
    public void deleteByNoticeIds(List<Integer> ids) {
        QueryWrapper<NoticeAdmin> query = new QueryWrapper<>();
        query.in("notice_id", ids);
        NoticeAdmin noticeAdmin = new NoticeAdmin();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);
        baseMapper.update(noticeAdmin, query);
    }

    @Override
    public int countReadByNoticeId(Integer noticeId) {
        QueryWrapper<NoticeAdmin> query = new QueryWrapper<>();
        query.eq("notice_id", noticeId);
        query.isNull("read_time");
        return baseMapper.selectCount(query);
    }

    @Override
    public void updateByNoticeId(NoticeAdmin noticeAdmin, Integer noticeId) {
        QueryWrapper<NoticeAdmin> query = new QueryWrapper<>();
        query.eq("notice_id", noticeId);

        noticeAdmin.setUpdateTime(LocalDateTime.now());
        baseMapper.update(noticeAdmin, query);
    }
}
